package ext.tianma.doc.util;

import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ptc.core.lwc.common.BaseDefinitionService;
import com.ptc.core.lwc.common.view.AttributeDefinitionReadView;
import com.ptc.core.lwc.common.view.ConstraintDefinitionReadView;
import com.ptc.core.lwc.common.view.EnumerationDefinitionReadView;
import com.ptc.core.lwc.common.view.ConstraintDefinitionReadView.RuleDataObject;
import com.ptc.core.lwc.common.view.EnumerationEntryReadView;
import com.ptc.core.lwc.common.view.PropertyValueReadView;
import com.ptc.core.lwc.common.view.TypeDefinitionReadView;
import com.ptc.core.lwc.server.TypeDefinitionServiceHelper;
import com.ptc.core.meta.common.TypeIdentifier;

import wt.access.NotAuthorizedException;
import wt.inf.container.WTContainerException;
import wt.services.ServiceFactory;
import wt.type.TypedUtility;
import wt.util.WTException;
/**
 * 获得地区枚举值
 * @author deng
 *
 */
@Controller
@RequestMapping({ "/GetResearchSite" })
public class GetResearchSite {
	public static final String ATTRIBUTE_NAME = "地区";//枚举值
	@RequestMapping(value = "/getResearchSite", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void getResearchSite(HttpServletResponse response) throws Exception {
		boolean enforce = wt.session.SessionServerHelper.manager
				.setAccessEnforced(false);
		PrintWriter out = response.getWriter();
		response.setContentType("text/html;charset=UTF-8");
		try {
			String result = "";
			JSONObject ob = new JSONObject();
			JSONArray array = new JSONArray();
			
			Map map = getEnumerationValues(ATTRIBUTE_NAME);//获得枚举值地区的值
			if(map.size()>0) {
				for(Object key : map.keySet()) {
					JSONObject ob1 = new JSONObject();
					String key1 = (String) key;
					String value =(String) map.get(key1);
					 ob.put("status", "success");
					   ob.put("message", "查询成功!");
					   ob1.put("researchName", key1);
					   ob1.put("showName", value);
					   array.put(ob1);
				}
				ob.put("data", array);
			}else {
				ob.put("status", "false");
				ob.put("message", "未查到！");
			}
			result = ob.toString();
			out.print(result);
		} catch (Throwable e) {
			e.printStackTrace();
		} finally {
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
	}
	
	/**
	 * 获取枚举值根据枚举名
	 * @param enumerationTypeName
	 * @return
	 */
	public static Map<String,String> getEnumerationValues( String enumerationTypeName ) {
		Map<String,String> enumerationValueMap = new HashMap<String,String>();
		try{
			BaseDefinitionService seviec = (BaseDefinitionService)ServiceFactory.getService(BaseDefinitionService.class);
			EnumerationDefinitionReadView view = seviec.getEnumDefView( enumerationTypeName );
			Map<String, EnumerationEntryReadView> map = view.getAllEnumerationEntries();
			Iterator<String> keyIte = map.keySet().iterator();
			while ( keyIte.hasNext()) {
				String key = keyIte.next();
				if( key ==null ) {
					continue;
				}
				EnumerationEntryReadView readview= map.get( key );
				if(readview==null) {
					continue;
				}
				if (readview.getPropertyValueByName("selectable").getValue().toString().equals("true")) {
					PropertyValueReadView propreadview = readview.getPropertyValueByName("displayName");
					if(propreadview==null||propreadview.getValueAsString()==null) {
						continue;
					}
					enumerationValueMap.put( key , propreadview.getValueAsString());
				}
			}
		} catch (NotAuthorizedException e) {
			e.printStackTrace();
		} catch (WTContainerException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}finally{
			
		}
		
		return enumerationValueMap ;
	}
	/**
	 * 根据枚举名和内部名称获得显示名称
	 * @param enumerationTypeName
	 * @param innerValue
	 * @return
	 */
	public static String getEnumerationDisplayValue( String enumerationTypeName , String innerValue ){
		Map<String,String> enumerationValues = getEnumerationValues( enumerationTypeName ) ;
		
		String enumerationDisplayValue = enumerationValues.get( innerValue ) ;
		
		return enumerationDisplayValue ;
	}
}
